package com.japstudy.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.japstudy.entity.WordSex;
import com.japstudy.service.IWordSexService;
import com.japstudy.util.PageUtil;
import com.japstudy.util.UUIDUtil;

@Controller
@RequestMapping("/WordSex")
public class WordSexController {

	@Autowired
	private IWordSexService wordSexServiceImpl;
	
	@RequestMapping("/wordSexAddUi")
	public String wordSexAddUi() {
		return "admin/wordSex_add";
	}
	
	@RequestMapping("/wordSexListUi")
	public String wordSexListUi() {
		return "admin/wordSex_list";
	}
	
	@RequestMapping("/pageCountChange")
	public @ResponseBody List<WordSex> pageCountChange(HttpServletRequest request, HttpServletResponse response, String pageCount, String nowPageNumber) {
		List<WordSex> wordSexs = (List<WordSex>) request.getSession().getAttribute("realWordSexsAdmin");
		int intPageCount = Integer.parseInt(pageCount);
		int intNowPageNumber = Integer.parseInt(nowPageNumber);
		int maxPage = PageUtil.getMaxPage(wordSexs, intPageCount);
		if(intNowPageNumber > maxPage) {
			intNowPageNumber = maxPage;
		}
		List<WordSex> pageData = PageUtil.getPageData(wordSexs, intNowPageNumber, intPageCount);
		return pageData;
	}
	
	@RequestMapping("/toPage")
	public @ResponseBody List<WordSex> toPage(HttpServletRequest request, HttpServletResponse response, String pageCount, String toPageNumber) {
		List<WordSex> wordSexs = (List<WordSex>) request.getSession().getAttribute("realWordSexsAdmin");
		int intPageCount = Integer.parseInt(pageCount);
		int intToPageNumber = Integer.parseInt(toPageNumber);
		List<WordSex> pageData = PageUtil.getPageData(wordSexs, intToPageNumber, intPageCount);
		return pageData;
	}
	
	@RequestMapping("/selectAll")
	public String selectAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<WordSex> wordSexs = wordSexServiceImpl.selectAll();
		List<WordSex> pageData = PageUtil.getPageData(wordSexs, 1, 10);
		request.getSession().setAttribute("realWordSexsAdmin", wordSexs);
		request.getSession().setAttribute("wordSexsAdmin", pageData);
		response.sendRedirect(request.getContextPath() + "/WordSex/wordSexListUi");
		return null;
	}
	
	@RequestMapping("/selectByNameLike")
	public String selectByNameLike(String wordSexName, HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<WordSex> wordSexs = wordSexServiceImpl.selectByNameLike(wordSexName);
		List<WordSex> pageData = PageUtil.getPageData(wordSexs, 1, 10);
		request.getSession().setAttribute("realWordSexsAdmin", wordSexs);
		request.getSession().setAttribute("wordSexsAdmin", pageData);
		response.sendRedirect(request.getContextPath() + "/WordSex/wordSexListUi");
		return null;
	}
	
	@RequestMapping("/haveSameWordSexName")
	public @ResponseBody String haveSameWordSexName(WordSex wordSex) {
		WordSex haveWordSex = wordSexServiceImpl.selectByWordSexName(wordSex);
		if(haveWordSex == null) {
			return "no";
		} else {
			return "yes";
		}
	}
	
	@RequestMapping("/wordSexAdd")
	public String wordSexAdd(WordSex wordSex, HttpServletRequest request, HttpServletResponse response) throws IOException {
		wordSex.setWordSexId(UUIDUtil.getUUID());
		if(1 == wordSexServiceImpl.add(wordSex)) {
			response.sendRedirect(request.getContextPath() + "/WordSex/selectAll");
			return null;
		} else {
			return null;
		}
	}
	
	@RequestMapping("/wordSexUpdateUi")
	public ModelAndView wordSexUpdateUi(String wordSexId){
		WordSex updateWordSex = wordSexServiceImpl.selectById(wordSexId);
		ModelAndView mav = new ModelAndView("admin/wordSex_update", "updateWordSexAdmin", updateWordSex);
		return mav;
	}
	
	@RequestMapping("/wordSexUpdate")
	public String wordSexUpdate(WordSex wordSex, HttpServletRequest request, HttpServletResponse response) throws IOException {
		int result = wordSexServiceImpl.update(wordSex);
		if(1 == result) {
			response.sendRedirect(request.getContextPath() + "/WordSex/selectAll");
		} else {
			response.sendRedirect(request.getContextPath() + "/WordSex/wordSexUpdateUi?wordSexId=" + wordSex.getWordSexId());
		}
		return null;
	}
	
	@RequestMapping("/delete")
	public String delete(WordSex wordSex, HttpServletRequest request, HttpServletResponse response) throws IOException {
		wordSexServiceImpl.delete(wordSex);
		response.sendRedirect(request.getContextPath() + "/WordSex/selectAll");
		return null;
	}
}
